/*
ssc install tabout, replace
ssc install reghdfe, replaces
ssc install frmttable, replace
*/

cd "~/Dropbox/china/Replication materials/"
***********
* Table 2 *
***********
use buildingcharacteristics, clear
gen Shop = (hasshop=="Y")*100
gen Window = (window=="Y")*100
gen plift = (lifts!=0)*100
replace yearbuilt = "1988.5" if yearbuilt == "1988-1989"
replace yearbuilt = "1992.5" if yearbuilt == "1992-1993"
replace yearbuilt = "1993" if yearbuilt == "1992-1994"
destring yearbuilt, replace force
tabstat floors_fixed aptperfloor entrances lifts plift stairwells Shop Window yearbuilt percentrelocated_fixed if floors_fixed!=0, s(n mean sd min max) c(statistics) save
matrix H = r(StatTotal)'
frmttable using "~/Dropbox/china/Replication materials/Table2.tex", ///
statmat(H) sdec(,0,2,2,0,0) ///
ctitle("Variable","Observations","Mean","S.D.","Min","Max") ///
 rtitle("Number of floors"\"Apartments per floor"\"Number of entrances"\"Number of elevators"\"\% with elevator(s)"\"Number of stairwells"\"With shop on first floor"\"Stairwell has window"\"Year built"\"\% relocated households") tex replace fr

***********
* Table 1 *
***********
use Dataset1, clear
label define T 1 "Regular China Post" 2 "Courier service" 3 "Courier service + urgent stamp" 4 "Target’s name, subject’s address" 5 "Placebo", replace
label value treatment_ T
tabulate treatment_ if treated_==1, matcell(Y)
matrix Y = [Y \ Y[1,1]+Y[2,1]+Y[3,1]+Y[4,1]+Y[5,1]]
frmttable using "~/Dropbox/China/Replication materials/Table1.tex", ///
statmat(Y) sdec(,0) ///
ctitle("Treatment","Observations") ///
 rtitle("Regular China Post"\"Courier service"\"Courier service + urgent stamp"\"Target’s name, subject’s address"\"Placebo"\"Total") tex replace fr


***********
* Table 3 *
***********
tab relocated, matcell(A)
matrix H16 = J(1,3,.)
matrix H16[1,1] = A[2,1]
matrix H16[1,2] = A[1,1]
matrix H16[1,3] = A[1,1]+A[2,1]
matrix H1 = J(1,3,.)
matrix H1[1,1] = A[2,1]/(A[1,1]+A[2,1])*100
matrix H1[1,2] = A[1,1]/(A[1,1]+A[2,1])*100
matrix H1[1,3] = 100
gen male_ = male*100
tabstat ages male_ yearmove previousfloors floors_fixed previousaptperf aptperfloor, by(relocated) save
matrix HH = [H1 \ [r(Stat2)' , r(Stat1)' , r(StatTotal)']]
gen options = min(floor(q7_1),4) if q7_1!=.
tab options if q7_1>0, matcell(A)
matrix A = 100*((A[1,1]+A[2,1]+A[3,1]+A[4,1])^(-1))*A
matrix A = [A, [.,.\.,.\.,.\.,.]]
gen anyoptions = q7_1>0 if q7_1!=.
sum anyoptions
scalar any = r(mean)*100
gen offeredcash = ((q_7_3_7_1!=""&q_7_3_7_1!="-9")+(q_7_3_7_2!=""&q_7_3_7_2!="-9")+(q_7_3_7_3!=""&q_7_3_7_3!="-9")+(q_7_3_7_4!=""&q_7_3_7_4!="-9")+(q_7_3_7_5!=""&q_7_3_7_5!="-9")>=1)
sum offeredcash if q_7_3_1_1!=""
scalar off = 100*r(mean)
gen own = (q_7_3_1_1=="2"|q_7_3_1_1=="own") if q_7_3_1_1!=""
sum own
scalar own = 100*r(mean)
matrix HH = [HH \ [.,.,.] \ [.,.,.] \ A \ [.,.,.] \ [any,.,.\off,.,.\own,.,.]\ [.,.,.]\H16]

frmttable using "~/Dropbox/china/Replication materials/Table3.tex", ///
statmat(HH) sdec(0\1\1\1\1\1\1\1\0\0\0\0\0\0\0\0\0) ///
ctitle("Variable","Relocated","Not relocated","Total") ///
 rtitle("Percent of sample"\"Age"\"Male"\"Year moved"\"Previous residence height"\"New residence height"\"Previous residence apts/floor"\"New residence apts/floor"\""\"\# housing options"\"One"\"Two"\"Three"\"Four or more"\" "\"Offered housing"\"Offered cash"\"Owner"\""\"Observations") tex replace fr

*************
* Figure A5 *
*************
gen number = (visit_rs_1!=.)+(visit_rs_2!=.)+(visit_rs_3!=.)+(visit_ts_final!=.)
gen hascounts = counts!=.

gen hascounts1 = hascounts*(number<=1)
gen hascounts2 = hascounts*(number<=2)
gen hascounts3 = hascounts*(number<=3)
gen hascounts4 = hascounts*(number<=4)

matrix C1 = J(4,3,.)
matrix C2 = J(4,3,.)
matrix C3 = J(4,3,.)
matrix C4 = J(4,3,.)
foreach i in 1 2 3 4 {
qui sum hascounts`i' if treatment==1 & number!=0
matrix C1[`i',1] = r(mean)
matrix C1[`i',2] = r(mean)-r(sd)/sqrt(r(N))
matrix C1[`i',3] = r(mean)+r(sd)/sqrt(r(N))
qui sum hascounts`i' if treatment==2 & number!=0
matrix C2[`i',1] = r(mean)
matrix C2[`i',2] = r(mean)-r(sd)/sqrt(r(N))
matrix C2[`i',3] = r(mean)+r(sd)/sqrt(r(N))
qui sum hascounts`i' if treatment==3 & number!=0
matrix C3[`i',1] = r(mean)
matrix C3[`i',2] = r(mean)-r(sd)/sqrt(r(N))
matrix C3[`i',3] = r(mean)+r(sd)/sqrt(r(N))
qui sum hascounts`i' if treatment==4 & number!=0
matrix C4[`i',1] = r(mean)
matrix C4[`i',2] = r(mean)-r(sd)/sqrt(r(N))
matrix C4[`i',3] = r(mean)+r(sd)/sqrt(r(N))
}
*
coefplot (matrix(C1[,1]), ci((C1[,2] C1[,3])) label(Post office) mc(blue) ciopt(lc(blue) recast(rcap)) m(S) lc(blue)) ///
(matrix(C2[,1]), ci((C2[,2] C2[,3])) label(Express) mc(black) ciopt(lc(black) recast(rcap)) m(Dh) lp(solid) lc(black)) ///
(matrix(C3[,1]), ci((C3[,2] C3[,3])) label(Urgent express) mc(black) ciopt(lc(black) lw(thin) recast(rcap)) m(Oh) lp(dash) lc(black)) ///
(matrix(C4[,1]), ci((C4[,2] C4[,3])) label(Urgent, wrong address) mc(red) ciopt(lc(red) lw(thin) recast(rcap)) m(Oh) lp(dash) lc(red)) ///
, vertical xtitle("") ///
coeflab(r1="First attempt" r2="at least 2" r3="at least 3" r4="at least 4") ///
 yline(0, lc(black) lw(vthin)) ///
 title("Response rate by number of attempts") ytitle(Response rate) fc(none) connect(l l l) xtitle() graphregion(color(white)) legend(r(2)) offset(0)
graph export "~/Dropbox/china/Replication materials/FigureA5.pdf", as(pdf) replace


************
* Table A1 *
************
* SELECTION EQUATIONS
logit responded MaleC treat3-treat5 i.commid  if treated==1
estimates store b1
logit responded MaleC treat3-treat5 i.commid haslift aptperfloor relocatedm if treated==1
estimates store b2
esttab b1 b2 using "~/Dropbox/china/Replication materials/TableA1.tex", wide width(\hsize) title("Survey responses") ///
cells(b(star fmt(3)) se(par)) stats(N r2_p, fmt(0 3) label(Observations R2)) ///
keep(treat3 treat4 treat5 haslift aptperfloor relocatedm) ///
legend label mtitle("Unrestricted" "Restricted") varlabels(_cons Constant treat3 "Express (T2)" treat4 "Urgent Express (T3)"  treat4 "Urgent Express - Mismatch (T4)") ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes addnotes("Community level dummies included" "* p$<$0.10, ** p$<$0.05, *** p$<$0.010") nonum collabels(none) replace


************
* Table A3 *
************
cd "~/Dropbox/china/Replication materials/"
use Dataset1, clear
gen floor_t = floor(apartment/100)
gen floor_s = floor(apt_s/100) 
gen number_t = apartment-100*floor_t
gen number_s = apt_s-100*floor_s
gen T = 0 if floor_t==floor_s
replace T = 1 if floor_t<floor_s & number_t==number_s
replace T = 2 if floor_t<floor_s & number_t!=number_s
gen T1 = T==1
gen T2 = T==2

reg sameyear ib0.T
estimates store b1
sum sameyear
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg sameorigin ib0.T
estimates store b2
sum sameorigin
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg sameheight ib0.T
estimates store b3
sum sameheight
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg sameaptperfloor ib0.T
estimates store b4
sum sameaptperfloor
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg bothdemolished ib0.T
estimates store b5
sum bothdemolished
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg diffexp ib0.T
estimates store b6
sum diffexp
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg samejobcia ib0.T
estimates store b7
sum samejobcia
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg samejob ib0.T
estimates store b8
sum samejob
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
reg samejobplace ib0.T
estimates store b9
sum samejobplace
estadd scalar mean = r(mean)
test 1.T 2.T
estadd scalar p_value = r(p)
esttab b1 b2 b3 b4 b5 b6 b7 b8 b9 using "~/Dropbox/china/Replication materials/TableA3_top.tex", wide width(\hsize) title("Difference in pairs characteristics by distance") ///
cells(b(star fmt(3)) se(par)) stats(N r2 F mean, fmt(0 3) label(Pairs R2 F-test Mean) star(F)) ///
legend label mtitle("Year moved" "Origin" "B. height" "No. apts" "Displaced" "Inc. gap" "Job descrip." "Employer" "Workplace") varlabels(_cons "Constant" 1bn.T "Next floor" 2.T "Next floor right") keep(_cons 1.T 2.T) ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes nonum r(coefs, transpose) ///
addnotes("* p$<$0.10, ** p$<$0.05, *** p$<$0.010") fragment ///
replace
***********************************
* per by building characteristics *
***********************************
reg sameyear i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b1
sum sameyear
estadd scalar mean = r(mean)
reg sameorigin  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b2
sum sameorigin
estadd scalar mean = r(mean)
reg sameheight  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b3
sum sameheight
estadd scalar mean = r(mean)
reg sameaptperfloor  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b4
sum sameaptperfloor
estadd scalar mean = r(mean)
reg bothdemolished  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b5
sum bothdemolished
estadd scalar mean = r(mean)
reg diffexp  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b6
sum diffexp
estadd scalar mean = r(mean)
reg samejobcia  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b7
sum samejobcia
estadd scalar mean = r(mean)
reg samejob  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b8
sum samejob
estadd scalar mean = r(mean)
reg samejobplace  i.commid i.piso#i.floors_fixed aptperfloor samefloorE
estimates store b9
sum samejobplace
estadd scalar mean = r(mean)
esttab b1 b2 b3 b4 b5 b6 b7 b8 b9 using "~/Dropbox/china/Replication materials/TableA3_bottom.tex", wide width(\hsize) title("Difference in pairs characteristics by distance") ///
cells(b(star fmt(3)) se(par)) stats(N r2 mean, fmt(0 3) label(Pairs R2 Mean)) keep(_cons aptperfloor) ///
legend label mtitle("Moved same year" "Same origin" "Same p. height" "Same p. apts" "Both displaced" "Inc. gap" "Same job" "Same employer" "Same workplace") varlabels(_cons "Constant" aptperfloor "Apartments per floor") ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes nonum r(coefs, transpose) ///
addnotes("* p$<$0.10, ** p$<$0.05, *** p$<$0.010") fragment ///
replace

************
* Table A2 *
************
cd "~/Dropbox/china/Replication materials/"
use Dataset1, clear
gen floor_t = floor(apartment/100)
gen floor_s = floor(apt_s/100) 
gen number_t = apartment-100*floor_t
gen number_s = apt_s-100*floor_s
gen T = 0 if floor_t==floor_s
replace T = 1 if floor_t<floor_s & number_t==number_s
replace T = 2 if floor_t<floor_s & number_t!=number_s
gen T1 = T==1
gen T2 = T==2
label variable age_t "Age (target)"
label variable gender_t "Male (target)"
label variable height "Building height"
label variable aptperfloor "Apartments per floor"
label variable Gender_s_ "Male (subject)"
label variable ages_s "Age (subject)"
label variable hhsize_s "HH size (subject)"
label variable yearmove_s "Year move (subject)"
label variable monthlyexp_s "Monthly expenses (subject)"
label variable wpeopleknown_s "People known (subject)"
label variable relocated_s "Relocated (subject)"
global DD "age_t gender_t height aptperfloor Gender_s_ ages_s hhsize_s yearmove_s monthlyexp_s wpeopleknown_s relocated_s"

balancetable (mean if T==0) (mean if T==1) (mean if T==2) (diff T1 if T!=2)  (diff T2 if T!=1)  (diff T1 if T!=0) ///
             $DD using "~/Dropbox/China/Replication materials/TableA2.tex" if counts!=. & treatment!=0, ///
            ctitles("Next door" "Next floor up" "Next floor right" "0 v 1" "0 v 2" "1 v 2") varlabels replace

***********
* Table 1 *
***********
cd "~/Dropbox/china/Replication materials/"
use Dataset1, clear
gen floor_t = floor(apartment/100)
gen floor_s = floor(apt_s/100) 
gen number_t = apartment-100*floor_t
gen number_s = apt_s-100*floor_s
gen T = 0 if floor_t==floor_s
replace T = 1 if floor_t<floor_s & number_t==number_s
replace T = 2 if floor_t<floor_s & number_t!=number_s
gen T1 = T==1
gen T2 = T==2
cap program drop miss
program miss
args lnf p m
tempvar p_return p_missclas prob
quietly {

gen double `p_return' = 1/(1+exp(-`p'))
gen double `p_missclas' = 1/(1+exp(-`m'))

gen double `prob' = (`p_missclas'^2)*`p_return'+`p_missclas'*(1-`p_return') if $ML_y1==0
replace `prob' = (1-`p_missclas')*(1-`p_return')+2*(`p_misclass'-`p_missclas'^2)*`p_return' if $ML_y1==1
replace `prob' = ((1-`p_missclas')^2)*`p_return' if $ML_y1==2

replace `lnf' = ln( `prob' )
}
end

global addcontr "relocated_sm yearmove_sm monthlyexp_sm onlyoneoffer_s oldtall_s oldapts_s"
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b1
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=50 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b2
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=90 & (counts==1 | counts==2), iter(100)
margins, dydx(*) post
estimates store b3
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr bothfriends aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b4
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr sameplace_ aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b5

************************
* Figure 2 * Panel (a) *
************************
coefplot (b1, label("All") mc(black*.25) msize(large) ciopt(lc(black*.25) lw(thick)) m(o)) ///
         (b2, label("At least 50% relocated") msize(large) mc(black*.5) ciopt(lc(black*.5) lw(thick)) m(o)) ///
         (b3, label("At least 90% relocated") msize(large) mc(black) ciopt(lc(black) lw(thick)) m(d)), ///
ytitle("Marginal effects", size(large)) yline(0, lc(black)) ///
		  title("") keep(aptperfloor samefloorE) graphregion(color(white)) ///
		  coeflab(aptperfloor="No. apartments. per floor" samefloorE="Neighbor next door" propsameold="Prop. old neighbors", labsize(large)) level(90) vert legend(size(large))
		  graph export "~/Dropbox/China/Replication materials/Figure2a.pdf", as(pdf) replace
*
ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(500)
estimates store b6
*
ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=50, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(5000) 
estimates store b7
*
ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=90, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(5000) 
estimates store b8
*
ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE bothfriends) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(5000) 
estimates store b9
*
ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE sameplace_) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(5000) 
estimates store b10
*

*************************
* Figure 2 * Panel (b) *
*************************
coefplot (b6, label("All") msize(large) mc(black*.25) ciopt(lc(black*.25) lw(thick)) m(o)) ///
         (b7, label("At least 50% relocated") msize(large) mc(black*.5) ciopt(lc(black*.5) lw(thick)) m(o)) ///
         (b8, label("At least 90% relocated") msize(large) mc(black) ciopt(lc(black) lw(thick)) m(d)), ///
		 vertical yline(0, lc(black)) xtitle("") ytitle("logit coefficients", size(large)) ///
		  title("") keep(aptperfloor samefloorE) graphregion(color(white)) ///
		  coeflab(aptperfloor="No. apartments. per floor" samefloorE="Neighbor next door" propsameold="Prop. old neighbors", labsize(large)) level(90) leg(size(large))
graph export "~/Dropbox/China/Replication materials/Figure2b.pdf", as(pdf) replace
label variable bothfriends "Both friends"
************
* Table A4 *
************
esttab b1 b2 b3 b4 b5 b6 b7 b8 b9 b10 using "~/Dropbox/China/Replication materials/TableA4.tex", wide width(\hsize) title("Survey measures of quality of social relationships") ///
cells(b(star fmt(3)) se(par)) stats(N r2_p N_clust, fmt(0 3 0) label(Observations R2 "Number of respondents" )) ///
legend label mtitle("All" "50\%" "90\%" "w/friends" "w/same origing" "All" "50\%" "90\%" "w/friends" "w/same origing") varlabels(_cons Constant aptperfloor "Apt. per floor" samefloorE "Same floor" relocated_sm "Subject relocated" yearmove_sm "Year of move") ///
keep(samefloorE aptperfloor relocated_sm yearmove_sm) ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes nonum fr replace

************
* Table A5 *
************
****************
* DEMOGRAPHICS *
****************
cap drop temp1
gen temp1 = aptperfloor*MaleS
cap drop temp2
gen temp2 = aptperfloor*ages
cap drop temp3
gen temp3 = samefloorE*c.MaleS
cap drop temp4
gen temp4 = samefloorE*ages
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE MaleS ages if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store B1
ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE MaleS ages) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(5000)
estimates store B2
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE MaleS ages temp1 temp2 temp3 temp4 if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store B3
qui ml model lf miss (p: counts = MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE MaleS ages temp1 temp2 temp3 temp4) (m: = i.treatment) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
ml max, difficult iterate(5000)
estimates store B4
esttab B1 B3 B2 B4 using "~/Dropbox/China/Replication materials/TableA5.tex", wide width(\hsize) title("Survey measures of quality of social relationships") ///
cells(b(star fmt(3)) se(par)) stats(N, fmt(0) label(Observations)) ///
legend label mtitle("All" "50\%" "90\%" "w/friends" "w/same origing" "All" "50\%" "90\%" "w/friends" "w/same origing") varlabels(_cons Constant ages "Age" MaleS "Male" relocated_sm "Subject relocated" yearmove_sm "Year of move" temp1 "Apt per floorXMale" temp2 "Apt per floorXAge" temp3 "Same floorXMale" temp4 "Same floorXAge" samefloorE "Same floor" aptperfloor "Apartments per floor") ///
keep(samefloorE aptperfloor ages MaleS relocated_sm yearmove_sm temp1 temp2 temp3 temp4) ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes nonum fr replace ///
addnotes("{Note: Regressions control for treatment assignment, community, apartment's floor$\times$building height, relocation status, year of move, monthly expenditures, receiving only one offer for relocation, and height and apartments per floor of previous dwelling. The left-hand side panel estimates exclude cases reporting receiving no envelopes ($N\leq 189$). The right-hand side panel accounts for miss-classification using the rate of false negatives estimated from envelopes delivered by the authors ($N\leq 258$). The model excludes the possibility of false positives since the placebo treatment refutes that case.}" "* p$<$0.10, ** p$<$0.05, *** p$<$0.010")

************
* Table A8 *
************
***************
* First stage *
***************
use "~/Dropbox/China/Replication materials/Dataset1.dta", clear
preserve
cap drop reloc
gen leave1 = 0 if q6==1
replace leave1 = 1 if q6==2
gen reloc1 = (q12==1|q13==1)
gen dist_to_work1 = ln(max(3,min(30,dist1))) if dist1!=.
gen exp1 = monthlyexp
gen APF1 = min(q14_2,10) if q14_2!=.
gen H1 = q14_1
gen piso_1 = max(1,floor(runiform()*H1))
gen leave2 = 0
gen reloc2 = relocated==1
gen dist_to_work2 = ln(max(3,min(30,dist2))) if dist2!=.
gen exp2 = monthlyexp
gen APF2 = aptperfloor
gen H2 = floors_fixed
gen piso_2 = piso
qui sum monthlyexp
replace exp1 = r(mean) if exp1==.
replace exp2 = r(mean) if exp2==.
duplicates report commidhhid, force
duplicates drop commidhhid, force
drop if q6==.
reshape long leave reloc dist_to_work APF H dist exp piso_, i(commidhhid) j(time)
cap drop stayed
gen stayed = 1-leave
qui sum stayed
scalar mm = r(mean)
cap drop w
gen w = (.75/mm)^(-1) if stayed==1
replace w = (.25/(1-mm))^(-1) if stayed==0
gen APFH = APF*H
probit stayed APF H APFH c.dist_to_work#i.commid i.commid reloc exp piso_ [pw=w]
margins, dydx(*) post
estimates store b0
gen www = APF*H
local bw=1/(476^(1/6.02))
probit stayed APF H APFH c.dist_to_work#i.commid i.commid reloc exp piso_ [pw=w]
test c.dist_to_work#15.commid c.dist_to_work#266.commid c.dist_to_work#471.commid c.dist_to_work#703.commid c.dist_to_work#833.commid c.dist_to_work#1164.commid c.dist_to_work#1250.commid
estimates store b0
restore
esttab b0 using "~/Dropbox/China/Replication materials/TableA8.tex", wide width(\hsize) title("Current and Past moves") starlevels( * 0.10 ** 0.05 *** 0.010) ///
cells(b(star fmt(3)) se(par)) stats(N, fmt(0 3 0) label(Observations R2 "Number of respondents" )) ///
legend label mtitle("Stayed") varlabels(_cons Constant APF "Apartments per floor" H "Building height" APFH "Apartments per floorXBuilding heigth" reloc "Relocated" exp "Monthly expenses" piso_ "Apartment floor") fr replace
************
* Table A9 *
************
****************
* Second stage *
****************
preserve 
gen dist_to_work = ln(max(3,min(30,dist4))) if dist4!=.
egen mm = mean(dist_to_work), by(commid)
replace dist_to_work = mm if dist_to_work==.
cap drop reloc
gen reloc = relocated_sm
gen exp = monthlyexp_sm
gen APF = aptperfloor
gen H = floors_fixed
gen APFH = APF*H
gen piso_ = piso if samefloorE==1
replace piso_ = piso+1 if samefloorE==0
predict error, xb
gen errorsq = error^2
predict controlf, p
gen controlfsq = controlf^2
gen error1 = normalden(error)/(1-normal(error))
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b1
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE error1 if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b2
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=50 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b3
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE error1 if treatment!=0 & percentrelocated_fixed>=50 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b4
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=90 & (counts==1 | counts==2)
margins, dydx(*) post
estimates store b5
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE error1 if treatment!=0 & percentrelocated_fixed>=90 & (counts==1 | counts==2), iter(100)
margins, dydx(*) post
estimates store b6
restore
coefplot (b1, label("All") mc(black*.5) ciopt(lc(black*.5)) m(o)) ///
         (b2, label("All+selection correction (sc)") mc(blue*.5) ciopt(lc(blue*.5)) m(o)) ///
         (b3, label("At least 50% relocated") mc(black) ciopt(lc(black)) m(d)) ///
		 (b4, label("At least 50% relocated + sc") mc(blue) ciopt(lc(blue)) m(d)) ///
         (b5, label("At least 90% relocated") mc(black) ciopt(lc(black)) m(d)) ///
		 (b6, label("At least 90% relocated + sc") mc(blue) ciopt(lc(blue)) m(d)), ///
		 vertical yline(0, lc(black)) xtitle("") ytitle("Marginal effects") ///
		  title("") keep(aptperfloor samefloorE) graphregion(color(white)) ///
		  coeflab(aptperfloor="No. apartments. per floor" samefloorE="Neighbor next door" propsameold="Prop. old neighbors") level(90)
graph export "~/Dropbox/China/Replication materials/FigureA6.pdf", as(pdf) replace
esttab b1 b2 b3 b4 b5 b6 using "~/Dropbox/China/Replication materials/TableA9.tex", wide width(\hsize) title("Survey measures of quality of social relationships") ///
cells(b(star fmt(3)) se(par)) stats(N, fmt(0 3 0) label(Observations R2 "Number of respondents" )) ///
legend label mtitle("All" "All" "50\%" "50\%" "90\%" "90\%") varlabels(_cons Constant error1 "Inverse Mill's ratio") ///
keep(samefloorE aptperfloor error1) ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes nonum fr replace

**********************************
* Figure 3 * Panels (a) and (b) *
**********************************
matrix A = J(100,6,.)
forvalues i = 1(1)100 {
constraint define 1 _b[bothfriends] = (`i'-1)/20
qui probit response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE bothfriends if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2), constraint(1)
qui margins, dydx(*) post
matrix myselect1 = r(table)["b","samefloorE"]
matrix myselect2 = r(table)["se","samefloorE"]
matrix myselect3 = r(table)["b","aptperfloor"]
matrix myselect4 = r(table)["se","aptperfloor"]
matrix myselect5 = r(table)["b","bothfriends"]
matrix A[`i',1] = (`i'-1)/20
matrix A[`i',2] = myselect1[1,1]
matrix A[`i',3] = myselect2[1,1]
matrix A[`i',4] = myselect3[1,1]
matrix A[`i',5] = myselect4[1,1]
matrix A[`i',6] = myselect5[1,1]
}
matrix B1 = J(50,3,.)
matrix B2 = J(50,3,.)
forvalues i=1(1)50{
	matrix B1[`i',1] = A[2*`i'-1,2]
	matrix B1[`i',2] = A[2*`i'-1,2]-1.645*A[2*`i'-1,3]
	matrix B1[`i',3] = A[2*`i'-1,2]+1.645*A[2*`i'-1,3]
	matrix B2[`i',1] = A[2*`i'-1,4]
	matrix B2[`i',2] = A[2*`i'-1,4]-1.645*A[2*`i'-1,5]
	matrix B2[`i',3] = A[2*`i'-1,4]+1.645*A[2*`i'-1,5]
}
***********************
* Figure 3, panel (a) *
***********************
* Neighbor next door
coefplot (matrix(B1[,1]), ci((B1[,2] B1[,3])) mc(black) ciopt(lc(black)) m(S) lc(black) label(2010)), ///
vertical yline(0, lc(black)) xtitle("") ///
coeflab(r1="0" r2=" " r3=" " r4=" " r5=" " r6=" " r7=" " r8=" " r9=" " r10=" " ///
        r11=".43" r12=" " r13=" " r14=" " r15=" " r16=" " r17=" " r18=" " r19=" " r20=" " ///
		r21=".43" r22=" " r23=" " r24=" " r25=" " r26=" " r27=" " r28=" " r29=" " r30=" " ///
		r31=".56" r32=" " r33=" " r34=" " r35=" " r36=" " r37=" " r38=" " r39=" " r40=" " ///
		r41=".66" r42=" " r43=" " r44=" " r45=" " r46=" " r47=" " r48=" " r49=" " r50=" " ///
		) ytitle("Marginal effect of neighbor next door", size(large)) ///
 fc(none) ciopts(recast(rcap)) graphregion(color(white)) ///
offset(0) xtitle("Marginal effect of being friends", size(large))
graph export "~/Dropbox/China/Replication materials/Figure3a.pdf", as(pdf) replace
************************
* Figure 3, panel (b) *
************************
* Apartments per floor
coefplot (matrix(B2[,1]), ci((B2[,2] B2[,3])) mc(black) ciopt(lc(black)) m(S) lc(black) label(2010)), ///
vertical yline(0, lc(black)) xtitle("") ///
coeflab(r1="0" r2=" " r3=" " r4=" " r5=" " r6=" " r7=" " r8=" " r9=" " r10=" " ///
        r11=".43" r12=" " r13=" " r14=" " r15=" " r16=" " r17=" " r18=" " r19=" " r20=" " ///
		r21=".43" r22=" " r23=" " r24=" " r25=" " r26=" " r27=" " r28=" " r29=" " r30=" " ///
		r31=".56" r32=" " r33=" " r34=" " r35=" " r36=" " r37=" " r38=" " r39=" " r40=" " ///
		r41=".66" r42=" " r43=" " r44=" " r45=" " r46=" " r47=" " r48=" " r49=" " r50=" " ///
		) ytitle("Marginal effect of apartments per floor", size(large)) ///
 fc(none) ciopts(recast(rcap)) graphregion(color(white)) ///
offset(0) xtitle("Marginal effect of being friends", size(large))
graph export "~/Dropbox/China/Replication materials/Figure3b.pdf", as(pdf) replace



************
* Table A6 *
************
cd "~/Dropbox/China/Replication materials/"
use Dataset2, clear
global cc "relocated yearmove monthlyexp onlyoneoffer oldtall oldapts propsameold"

probit stranger i.commid i.piso#i.floors_fixed MaleS agesm $cc aptperfloor samefloors if Relationship!=. & percentrelocated_fixed>=0 & commid+hhid!=., cluster(commidhhid)
margins, dydx(*) post
estimates store b1
probit stranger i.commid i.piso#i.floors_fixed MaleS agesm $cc aptperfloor samefloors if Relationship!=. & percentrelocated_fixed>=50 & commid+hhid!=., cluster(commidhhid)
margins, dydx(*) post
estimates store b2
probit stranger i.commid i.piso#i.floors_fixed MaleS agesm $cc aptperfloor samefloors if Relationship!=. & percentrelocated_fixed>=90 & commid+hhid!=., cluster(commidhhid)
margins, dydx(*) post
estimates store b3
**************************
* Figure 2 * Panel (c) *
**************************
coefplot (b1, label("All") mc(black*.25) msize(large) ciopt(lc(black*.25) lw(thick)) m(o)) ///
         (b2, label("At least 50% relocated") msize(large)  mc(black*.5) ciopt(lc(black*.5) lw(thick)) m(o)) ///
         (b3, label("At least 90% relocated") msize(large)  mc(black) ciopt(lc(black) lw(thick)) m(d)), ///
		 vertical yline(0, lc(black)) xtitle("") ytitle("Marginal effects", size(large)) ///
		  title("") keep(aptperfloor samefloors) graphregion(color(white)) legend(size(large)) ///
		  coeflab(aptperfloor="No. apartments. per floor" samefloors="Neighbor next door" propsameold="Prop. old neighbors", labsize(large)) level(90)
graph export "~/Dropbox/China/Replication materials/Figure2c.pdf", as(pdf) replace
* Smiles and nods
probit node i.commid i.piso#i.floors_fixed MaleS agesm $cc aptperfloor samefloors if Relationship!=. & percentrelocated_fixed>=0 & commid+hhid!=., cluster(commidhhid)
margins, dydx(*) post
estimates store b4
probit node i.commid i.piso#i.floors_fixed MaleS agesm $cc aptperfloor samefloors if Relationship!=. & percentrelocated_fixed>=50 & commid+hhid!=., cluster(commidhhid)
margins, dydx(*) post
estimates store b5
probit node i.commid i.piso#i.floors_fixed MaleS agesm $cc aptperfloor samefloors if Relationship!=. & percentrelocated_fixed>=90 & commid+hhid!=., cluster(commidhhid)
margins, dydx(*) post
estimates store b6
*************************
* Figure 2 * Panel (d) *
*************************
coefplot (b4, label("All") mc(black*.25) msize(large) ciopt(lc(black*.25) lw(thick)) m(o)) ///
         (b5, label("At least 50% relocated") msize(large)  mc(black*.5) ciopt(lc(black*.5) lw(thick)) m(o)) ///
         (b6, label("At least 90% relocated") msize(large)  mc(black) ciopt(lc(black) lw(thick)) m(d)), ///
		 vertical yline(0, lc(black)) xtitle("") ytitle("Marginal effects", size(large)) ///
		  title("") keep(aptperfloor samefloors) graphregion(color(white)) legend(size(large)) ///
		  coeflab(aptperfloor="No. apartments. per floor" samefloors="Neighbor next door" propsameold="Prop. old neighbors", labsize(large)) level(90)
graph export "~/Dropbox/China/Replication materials/Figure2d.pdf", as(pdf) replace
*
esttab b1 b2 b3 b4 b5 b6 using "~/Dropbox/China/Replication materials/TableA6.tex", wide width(\hsize) title("Survey measures of quality of social relationships") ///
cells(b(star fmt(3)) se(par)) stats(N N_clust, fmt(0 3 0) label(Observations "Number of respondents" )) ///
legend label mtitle("All" "50\%+ rell." "90\%+ rell." "All" "50\%+ rell." "90\%+ rell.") varlabels(_cons Constant) ///
keep(samefloors aptperfloor relocated yearmove) ///
starlevels( * 0.10 ** 0.05 *** 0.010) ///
nonotes nonum replace

***********
* Table 4 *
***********
cd "~/Dropbox/China/Replication materials/"
use Dataset3, clear
*************************
* AVOIDING INTERFERENCE *
*************************
gen PISO = floor(piso/2)
keep if counts!=. & treatment!=0
gen apt_t = apartment
egen location = group(address)
gen floor_t = floor(apartment/100)
gen floor_s = floor(apt_s/100) 
gen number_t = apartment-100*floor_t
gen number_s = apt_s-100*floor_s
gen T = 0 if floor_t==floor_s
replace T = 1 if floor_t<floor_s & number_t==number_s
replace T = 2 if floor_t<floor_s & number_t!=number_s
gen t1 = T==1
gen t2 = T==2
gen notsamefloorE = 1-samefloorE

sort location building apartment
egen B = group(location building)
egen envelopesperbuilding = count(counts) if treatment!=0, by(B)
sort B apartment
bysort B: gen sequence = _n
tsset B sequence
*
* Brute force DA algorithm, taking advantage of the structure of the data
* By construction, the DA will stop at the second round at the latest since there is always a second available match
*
gen f0 = apartment+(1-2*(number_t==aptperfloor))
gen f1 = apartment+100
gen f2 = apartment+100+(1-2*(number_t==aptperfloor))
gen dT0 = F1.apartment==f0 /*treatment T=0 that cannot be assigned*/
gen dT1 = F1.apartment==f1 /*treatment T=1 that cannot be assigned*/
gen dT2 = F1.apartment==f2 /*treatment T=2 that cannot be assigned*/
gen tie = F1.f0==f0 | F1.f1==f0 | F1.f2==f0 | F1.f0==f1 | F1.f1==f1 | F1.f2==f1 | F1.f0==f2 | F1.f1==f2 | F1.f2==f2 | ///
          L1.f0==f0 | L1.f1==f0 | L1.f2==f0 | L1.f0==f1 | L1.f1==f1 | L1.f2==f1 | L1.f0==f2 | L1.f1==f2 | L1.f2==f2
egen AA = group(q111) if q111!=""
cap drop sameF sameU
gen sameF = (B==L1.B)*(AA==L1.AA)*(number_t==L1.number_t)+(B==L1.B)*(AA==L1.AA)*(number_t==F1.number_t)>0 if AA!=.
gen sameU = (B==L1.B)*(AA==L1.AA)*(number_t!=L1.number_t)+(B==L1.B)*(AA==L1.AA)*(number_t!=F1.number_t)>0 if AA!=.
ttest sameF==sameU
*
* Dropping those with less than 3 options
cap drop only2
gen only2 = 1 if B==1 & apartment==101
replace only2 = 1 if apartment==101 & B==1
replace only2 = 1 if apartment==406 & B==1
replace only2 = 1 if apartment==506 & B==4
replace only2 = 1 if apartment==1104 & B==9
replace only2 = 1 if apartment==102 & B==61
replace only2 = 1 if apartment==301 & B==64
replace only2 = 1 if apartment==201 & B==73
replace only2 = 1 if apartment==105 & B==96
replace only2 = 1 if apartment==402 & B==106
replace only2 = 1 if apartment==401 & B==123
replace only2 = 1 if apartment==402 & B==123
replace only2 = 1 if apartment==501 & B==126
replace only2 = 1 if apartment==502 & B==126
replace only2 = 1 if apartment==101 & B==138
replace only2 = 1 if apartment==102 & B==138
replace only2 = 1 if apartment==202 & B==139
replace only2 = 1 if apartment==102 & B==143
recode only2 . = 0
********************
* Main regressions *
********************
matrix A = J(5,5,.)
matrix B = J(4,5,.)
qui reg counts ib(1).treatment t1 t2 i.commid i.floors_fixed i.PISO
matrix A[1,1] = _b[i(2).treatment]
matrix A[2,1] = _b[i(3).treatment]
matrix A[3,1] = _b[i(4).treatment]
matrix A[4,1] = _b[t1]
matrix A[5,1] = _b[t2]
matrix A[1,2] = 2 * ttail(e(df_r), abs(_b[i(2).treatment]/_se[i(2).treatment]))
matrix A[2,2] = 2 * ttail(e(df_r), abs(_b[i(3).treatment]/_se[i(3).treatment]))
matrix A[3,2] = 2 * ttail(e(df_r), abs(_b[i(4).treatment]/_se[i(4).treatment]))
matrix A[4,2] = 2 * ttail(e(df_r), abs(_b[t1]/_se[t1]))
matrix A[5,2] = 2 * ttail(e(df_r), abs(_b[t2]/_se[t2]))
qui reg counts ib(1).treatment notsamefloorE i.commid i.floors_fixed i.PISO
matrix B[1,1] = _b[i(2).treatment]
matrix B[2,1] = _b[i(3).treatment]
matrix B[3,1] = _b[i(4).treatment]
matrix B[4,1] = _b[notsamefloorE]
matrix B[1,2] = 2 * ttail(e(df_r), abs(_b[i(2).treatment]/_se[i(2).treatment]))
matrix B[2,2] = 2 * ttail(e(df_r), abs(_b[i(3).treatment]/_se[i(3).treatment]))
matrix B[3,2] = 2 * ttail(e(df_r), abs(_b[i(4).treatment]/_se[i(4).treatment]))
matrix B[4,2] = 2 * ttail(e(df_r), abs(_b[notsamefloorE]/_se[notsamefloorE]))
******** Additional regression ********
gen cuatro = treatment==4
reghdfe counts samefloorE i.treatment i.treatment#c.samefloorE, a(PISO floors_fixed commid)
reghdfe counts samefloorE i.treatment c.cuatro#c.samefloorE, a(PISO floors_fixed commid)
reghdfe counts t1 t2 i.treatment i.treatment#c.t1 i.treatment#c.t2, a(PISO floors_fixed commid)
*********************************************
* SAMPLE IN WHICH PERMUTATION TEST IS VALID *
*********************************************
* Naive Permutation
permute counts _b[t1] _b[t2] _b[2.treatment] _b[3.treatment] _b[4.treatment], reps(1000) seed(12345):  reghdfe counts t1 t2 ib(1).treatment, a(PISO floors_fixed commid)
matrix A[1,3] = r(p_twosided)[1,3]
matrix A[2,3] = r(p_twosided)[1,4]
matrix A[3,3] = r(p_twosided)[1,5]
matrix A[4,3] = r(p_twosided)[1,1]
matrix A[5,3] = r(p_twosided)[1,2]
permute counts _b[notsamefloorE] _b[2.treatment] _b[3.treatment] _b[4.treatment], reps(1000) seed(12345):  reghdfe counts notsamefloorE  ib(1).treatment, a(PISO floors_fixed commid)
matrix B[1,3] = r(p_twosided)[1,2]
matrix B[2,3] = r(p_twosided)[1,3]
matrix B[3,3] = r(p_twosided)[1,4]
matrix B[4,3] = r(p_twosided)[1,1]
* Conservative permutation
permute counts _b[t1] _b[t2] _b[2.treatment] _b[3.treatment] _b[4.treatment], reps(1000) seed(12345):  reghdfe counts t1 t2 ib(1).treatment if only2==0, a(PISO floors_fixed commid)
matrix A[1,4] = r(p_twosided)[1,3]
matrix A[2,4] = r(p_twosided)[1,4]
matrix A[3,4] = r(p_twosided)[1,5]
matrix A[4,4] = r(p_twosided)[1,1]
matrix A[5,4] = r(p_twosided)[1,2]
permute counts _b[notsamefloorE] _b[2.treatment] _b[3.treatment] _b[4.treatment], reps(1000) seed(12345):  reghdfe counts notsamefloorE ib(1).treatment if only2==0, a(PISO floors_fixed commid)
matrix B[1,4] = r(p_twosided)[1,2]
matrix B[2,4] = r(p_twosided)[1,3]
matrix B[3,4] = r(p_twosided)[1,4]
matrix B[4,4] = r(p_twosided)[1,1]
**********************
* Randomization test *
**********************
*
capture program drop myboot
program define myboot, rclass
preserve 
* creates either a random number betwee 0-1 or -1 if the treatment cannot be assigned
* This prevents that a violation of interference occurs
gen x0 = uniform()
replace x0 = . if dT0 == 1
gen x1 = uniform()
replace x1 = . if dT1 == 1
gen x2 = uniform()
replace x2 = . if dT2 == 1
* minimal priority
egen min = rmin(x0 x1 x2)
* maximal priority
egen max = rmax(x0 x1 x2)
gen x3 = uniform()
* fake new matches
gen newmatch = .
replace newmatch = f0 if x0==max/*take T=0 as default*/
replace newmatch = f1 if x1==max /*replace with T=1 if T=1 has higher priority*/
replace newmatch = f2 if x2==max /*replace with T=2 if has highest priority*/
gen pair = 1
gen spair = 1
* fake second matches
gen newmatch2 = .
replace newmatch2 = f0 if x0==min /*take T=0 as default*/
replace newmatch2 = f1 if x1==min /*replace with T=1 if T=1 has higher priority*/
replace newmatch2 = f2 if x2==min /*replace with T=2 if has highest priority*/
gen snewmatch = newmatch2
* fake third matches
gen newmatch3 = .
replace newmatch3 = f0 if x0!=min & x0!=max /*take T=0 as default*/
replace newmatch3 = f1 if x0!=min & x0!=max /*replace with T=1 if T=1 has higher priority*/
replace newmatch3 = f2 if x0!=min & x0!=max /*replace with T=2 if has highest priority*/
replace newmatch3 = . if x0+x1+x2==.
sum snewmatch
return scalar N = r(N)
* Detecting ties
egen rank3 = rank(x3), by(B newmatch) /*This number should be unique unless there is a conflict*/
replace newmatch = newmatch2 if rank3!=1 /*This replaces the treatment of the target with the lowest rank */
replace snewmatch = newmatch3 if rank3!=1 /*This replaces the treatment of the target with the lowest rank */
replace pair = 2 if newmatch!=newmatch2
replace spair = 2 if newmatch!=newmatch2
egen srank3 = rank(x3), by(B newmatch2) /*This number should be unique unless there is a conflict*/
replace snewmatch = newmatch3 if srank3!=1 /*This replaces the treatment of the target with the lowest rank */
replace spair = 2 if snewmatch!=newmatch3
* Detecting more ties
egen rank4 = rank(x3), by(B newmatch)
*replace newmatch = newmatch2 if rank4!=1 & wife==1
replace newmatch = newmatch3 if rank4!=1 /*& wife==2*/
replace snewmatch = . if rank4!=1
*sum snewmatch
*return scalar N = r(N)
replace pair = 3 if newmatch!=newmatch3
* tie checks that the same subject is not assigned twice
cap drop tie
gen tie = newmatch==F1.newmatch | newmatch==L1.newmatch
sum tie
return scalar tie = r(sum)
gen tie2 = snewmatch==F1.snewmatch | snewmatch==L1.snewmatch
sum tie2
return scalar tie2 = r(sum)
*
* Generating new treatment assignment
*
gen floor_fs = floor(newmatch/100) 
gen number_fs = newmatch-100*floor_fs
gen Tf = 0 if floor_t==floor_fs & newmatch!=.
replace Tf = 1 if floor_t<floor_fs & number_t==number_fs & newmatch!=.
replace Tf = 2 if floor_t<floor_fs & number_t!=number_fs & newmatch!=.
gen Ef = Tf!=0 & snewmatch!=.
gen T1 = Tf==1 & snewmatch!=.
gen T2 = Tf==2 & snewmatch!=.
* Permuting treatments
generate double u1=runiform()
gen utreatment = 1 if u1<.2713
replace utreatment = 2 if u1>=.2713 & u1<.5194
replace utreatment = 3 if u1>=.5194 & u1<.7868
replace utreatment = 4 if u1>=.7868
*
reg counts ib(1).utreatment T1 T2 i.commid i.floors_fixed i.PISO if snewmatch!=F1.snewmatch & snewmatch!=.
return scalar t2 = _b[i(2).utreatment]/_se[i(2).utreatment]
return scalar t3 = _b[i(3).utreatment]/_se[i(3).utreatment]
return scalar t4 = _b[i(4).utreatment]/_se[i(4).utreatment]
return scalar a1 = _b[T1]/_se[T1]
return scalar a2 = _b[T2]/_se[T2]
reg counts ib(1).utreatment Ef i.commid i.floors_fixed i.PISO if snewmatch!=F1.snewmatch & snewmatch!=.
return scalar T2 = _b[i(2).utreatment]/_se[i(2).utreatment]
return scalar T3 = _b[i(3).utreatment]/_se[i(3).utreatment]
return scalar T4 = _b[i(4).utreatment]/_se[i(4).utreatment]
return scalar b = _b[Ef]/_se[Ef]
return scalar S = e(N)
end
qui reg counts ib(1).treatment t1 t2 i.commid i.floors_fixed i.PISO
scalar Rt2 = _b[i(2).treatment]/_se[i(2).treatment]
scalar Rt3 = _b[i(3).treatment]/_se[i(3).treatment]
scalar Rt4 = _b[i(4).treatment]/_se[i(4).treatment]
scalar Ra1 = _b[t1]/_se[t1]
scalar Ra2 = _b[t2]/_se[t2]
qui reg counts ib(1).treatment notsamefloorE i.commid i.floors_fixed i.PISO
scalar RT2 = _b[i(2).treatment]/_se[i(2).treatment]
scalar RT3 = _b[i(3).treatment]/_se[i(3).treatment]
scalar RT4 = _b[i(4).treatment]/_se[i(4).treatment]
scalar Rb = _b[notsamefloorE]/_se[notsamefloorE]
preserve
simulate a1 = r(a1) a2 = r(a2) b = r(b) t2=r(t2) t3=r(t3) t4=r(t4) T2=r(T2) T3=r(T3) T4=r(T4) tie=r(tie) tie2=r(tie2) S=r(S) N=r(N), reps(11000) seed(12345): myboot
keep if tie==0
keep if _n<=11000
local i = 1
foreach x in t2 t3 t4 a1 a2{
cap drop pos neg
gen pos = (`x'>R`x')
gen neg = (`x'<R`x')
qui sum pos
scalar pos = r(mean) 
qui sum neg
scalar neg = r(mean) 
matrix A[`i',5] = 2*min(pos,neg)
local ++i
}
local i = 1
foreach x in T2 T3 T4 a1{
cap drop pos neg
gen pos = (`x'>R`x')
gen neg = (`x'<R`x')
qui sum pos
scalar pos = r(mean) 
qui sum neg
scalar neg = r(mean) 
matrix B[`i',5] = 2*min(pos,neg)
local ++i
}
restore
**************
* MAIN TABLE *
**************
matrix H1 = A
matrix H2 = B
matrix rownames H1 = "Express (T2)" "Urgent Express (T3)" "Urgent Express - Mismatch (T4)" "Apartment right above" "Apartment above right"
matrix rownames H2 = "Express (T2)" "Urgent Express (T3)" "Urgent Express - Mismatch (T4)" "Apartment above"
matrix H = [[H1\[.,.,.,.,.]],[H1[1..3,1..5]\[.,.,.,.,.]\[.,.,.,.,.]\H2[4..4,1..5]]]
matrix rownames H = "Express (T2)" "Urgent Express (T3)" "Urgent Express - Mismatch (T4)" "Apartment right above" "Apartment above right" "Apartment above"
frmttable using "~/Dropbox/China/Replication materials/Table4.tex", statmat(H) ///
ctitle("","Model 1","(1)","(2)","(3)","(4)","Model 2","(1)","(2)","(3)","(4)") ///
rtitle("Express (T2)"\"Urgent Express (T3)"\"Urgent Express - Mismatch (T4)"\"Apartment right above"\"Apartment above right"\"Apartment above") replace fr tex  nobl sdec(3)

************
* Table A7 *
************
cd "~/Dropbox/China/Replication materials/"
use Dataset3, clear
cap drop PISO
gen PISO = floor(piso/2)
global addcontr "relocated_sm yearmove_sm monthlyexp_sm onlyoneoffer_s oldtall_s oldapts_s"
set varabbrev off

cap program drop miss
program miss
args lnf p m
tempvar p_return p_missclas prob
quietly {

gen double `p_return' = 1/(1+exp(-`p'))
gen double `p_missclas' = 1/(1+exp(-`m'))

gen double `prob' = (`p_missclas'^2)*`p_return'+`p_missclas'*(1-`p_return') if $ML_y1==0
replace `prob' = (1-`p_missclas')*(1-`p_return')+2*(`p_misclass'-`p_missclas'^2)*`p_return' if $ML_y1==1
replace `prob' = ((1-`p_missclas')^2)*`p_return' if $ML_y1==2

replace `lnf' = ln( `prob' )
}
end

* Data preparation
cap drop age_subject
gen age_subject = ages
replace age_subject = age_t if age_subject==.

* Data preparation
egen prelocated = mean(relocated), by(commid percentrelocated_fixed)
cap drop monthly
egen monthly = mean(monthlyexp),by(commid address2 building)
cap drop monthly1
egen monthly1 = mean(monthlyexp) if relocated==1,by(commid address2 building)
cap drop monthly0
egen monthly0 = mean(monthlyexp) if relocated==0,by(commid address2 building)
cap drop yearm
egen yearm = mean(yearmove),by(commid address2 building)
cap drop yearm1
egen yearm1 = mean(yearmove) if relocated==1,by(commid address2 building)
cap drop yearm0
egen yearm0 = mean(yearmove) if relocated==0,by(commid address2 building)
cap drop ppercent
gen ppercent = percentrelocated_fixed/100

* UPPER BOUND-LOWER BOUND
cap drop apt_fake0U_
gen apt_fake0U_ = .
cap drop same_fake0U_
gen same_fake0U_ = .
cap drop apt_fake50U_
gen apt_fake50U_ = .
cap drop same_fake50U_
gen same_fake50U_ = .
cap drop apt_fake90U_
gen apt_fake90U_ = .
cap drop same_fake90U_
gen same_fake90U_ = .
cap drop apt_fake0U
gen apt_fake0U = .
cap drop same_fake0U
gen same_fake0U = .
cap drop apt_fake50U
gen apt_fake50U = .
cap drop same_fake50U
gen same_fake50U = .
cap drop apt_fake90U
gen apt_fake90U = .
cap drop same_fake90U
gen same_fake90U = .
cap drop apt_fake0L_
gen apt_fake0L_ = .
cap drop same_fake0L_
gen same_fake0L_ = .
cap drop apt_fake50L_
gen apt_fake50L_ = .
cap drop same_fake50L_
gen same_fake50L_ = .
cap drop apt_fake90L_
gen apt_fake90L_ = .
cap drop same_fake90L_
gen same_fake90L_ = .
cap drop apt_fake0L
gen apt_fake0L = .
cap drop same_fake0L
gen same_fake0L = .
cap drop apt_fake50L
gen apt_fake50L = .
cap drop same_fake50L
gen same_fake50L = .
cap drop apt_fake90L
gen apt_fake90L = .
cap drop same_fake90L
gen same_fake90L = .
*
forvalues x = 1/1000{
preserve
cap drop fakedecision
gen fakedecision = counts
cap drop fakedecision_
gen fakedecision_ = counts
keep if counts!=.
cap drop noise1 noise2
set seed `x'
gen noise1 = runiform()
gen noise2 = runiform()
cap drop reloc
gen reloc = relocated_s
replace reloc = noise1<=(prelocated+ppercent)/2 if reloc==.&counts!=.
cap drop wip
gen wip = 1 if reloc==0
replace fakedecision = 1 if wip==1&counts!=0
replace fakedecision_ = 2 if wip==1&counts!=0
replace yearmove_sm = yearm1 if yearmove_s==.&yearm1!=.
replace yearmove_sm = yearm if yearmove_s==.&yearm1==.
replace monthlyexp_sm = monthly1 if monthlyexp_s==.&monthly1!=.
replace monthlyexp_sm = monthly if monthlyexp_s==.&monthly1==.
*
cap drop response1_
gen response1_ = 0 if fakedecision==1
replace response1_ = .33 if fakedecision==0
replace response1_ = .66 if fakedecision==. & treated==1
replace response1_ = 1 if fakedecision==2
*
cap drop response1__
gen response1__ = 0 if fakedecision_==1
replace response1__ = .33 if fakedecision_==0
replace response1__ = .66 if fakedecision_==. & treated==1
replace response1__ = 1 if fakedecision_==2
*
* upper bound
qui probit response1_ MaleC i.treatment i.commid i.piso#i.floors_fixed yearmove_sm monthlyexp_sm aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2), iterate(50)
qui margins, dydx(*) post
estimates store b1
qui probit response1_ MaleC i.treatment i.commid i.piso#i.floors_fixed yearmove_sm monthlyexp_sm aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=50 & (counts==1 | counts==2), iterate(50)
qui margins, dydx(*) post
estimates store b2
qui probit response1_ MaleC i.treatment i.commid i.piso#i.floors_fixed yearmove_sm monthlyexp_sm aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=90 & (counts==1 | counts==2), iterate(50)
qui margins, dydx(*) post
estimates store b3
ml model lf miss (p: fakedecision = MaleC i.treatment i.commid i.piso#i.floors_fixed /*relocated_sm*/ yearmove_sm monthlyexp_sm aptperfloor samefloorE) (m: = ) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(50)
estimate store b0
ml model lf miss (p: fakedecision = MaleC i.treatment i.commid i.piso#i.floors_fixed /*relocated_sm*/ yearmove_sm monthlyexp_sm aptperfloor samefloorE) (m: = ) if treatment!=0 & percentrelocated_fixed>=50, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(50)
estimate store b50
ml model lf miss (p: fakedecision = MaleC i.treatment i.commid i.piso#i.floors_fixed /*relocated_sm*/ yearmove_sm monthlyexp_sm aptperfloor samefloorE) (m: = ) if treatment!=0 & percentrelocated_fixed>=90, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(50)
estimate store b90
* lower bound
qui probit response1__ MaleC i.treatment i.commid i.piso#i.floors_fixed yearmove_sm monthlyexp_sm aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2), iterate(50)
qui margins, dydx(*) post
estimates store b1_
qui probit response1__ MaleC i.treatment i.commid i.piso#i.floors_fixed yearmove_sm monthlyexp_sm aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=50 & (counts==1 | counts==2), iterate(50)
qui margins, dydx(*) post
estimates store b2_
qui probit response1__ MaleC i.treatment i.commid i.piso#i.floors_fixed yearmove_sm monthlyexp_sm aptperfloor samefloorE if treatment!=0 & percentrelocated_fixed>=90 & (counts==1 | counts==2), iterate(50)
qui margins, dydx(*) post
estimates store b3_
ml model lf miss (p: fakedecision_ = MaleC i.treatment i.commid i.piso#i.floors_fixed /*relocated_sm*/ yearmove_sm monthlyexp_sm aptperfloor samefloorE) (m: = ) if treatment!=0 & percentrelocated_fixed>=0, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(50)
estimate store b0_
ml model lf miss (p: fakedecision_ = MaleC i.treatment i.commid i.piso#i.floors_fixed /*relocated_sm*/ yearmove_sm monthlyexp_sm aptperfloor samefloorE) (m: = ) if treatment!=0 & percentrelocated_fixed>=50, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(50)
estimate store b50_
ml model lf miss (p: fakedecision_ = MaleC i.treatment i.commid i.piso#i.floors_fixed /*relocated_sm*/ yearmove_sm monthlyexp_sm aptperfloor samefloorE) (m: = ) if treatment!=0 & percentrelocated_fixed>=90, technique(nr) cluster(commid)
ml init /p = -.4
ml init /m = 0
qui ml max, difficult iterate(50)
estimate store b90_
restore
estimates restore b1
replace apt_fake0U_ = _b[aptperfloor] if _n==`x'
replace same_fake0U_ = _b[samefloorE] if _n==`x'
estimates restore b2
replace apt_fake50U_ = _b[aptperfloor] if _n==`x'
replace same_fake50U_ = _b[samefloorE] if _n==`x'
estimates restore b3
replace apt_fake90U_ = _b[aptperfloor] if _n==`x'
replace same_fake90U_ = _b[samefloorE] if _n==`x'
estimates restore b0
replace apt_fake0U = _b[aptperfloor] if _n==`x'
replace same_fake0U = _b[samefloorE] if _n==`x'
estimates restore b50
replace apt_fake50U = _b[aptperfloor] if _n==`x'
replace same_fake50U = _b[samefloorE] if _n==`x'
estimates restore b90
replace apt_fake90U = _b[aptperfloor] if _n==`x'
replace same_fake90U = _b[samefloorE] if _n==`x'
estimates restore b1_
replace apt_fake0L_ = _b[aptperfloor] if _n==`x'
replace same_fake0L_ = _b[samefloorE] if _n==`x'
estimates restore b2_
replace apt_fake50L_ = _b[aptperfloor] if _n==`x'
replace same_fake50L_ = _b[samefloorE] if _n==`x'
estimates restore b3_
replace apt_fake90L_ = _b[aptperfloor] if _n==`x'
replace same_fake90L_ = _b[samefloorE] if _n==`x'
estimates restore b0_
replace apt_fake0L = _b[aptperfloor] if _n==`x'
replace same_fake0L = _b[samefloorE] if _n==`x'
estimates restore b50_
replace apt_fake50L = _b[aptperfloor] if _n==`x'
replace same_fake50L = _b[samefloorE] if _n==`x'
estimates restore b90_
replace apt_fake90L = _b[aptperfloor] if _n==`x'
replace same_fake90L = _b[samefloorE] if _n==`x'
}

matrix B = J(6,3,.)
local i = 1
foreach x in apt_fake0U_ apt_fake50U_ apt_fake90U_ apt_fake0U apt_fake50U apt_fake90U{
qui sum `x', d
matrix B[`i',1] = r(mean)
matrix B[`i',2] = r(p5)
matrix B[`i',3] = r(p95)
local ++i
}
matrix A = J(6,3,.)
local i = 1
foreach x in apt_fake0L_ apt_fake50L_ apt_fake90L_ apt_fake0L apt_fake50L apt_fake90L{
qui sum `x', d
matrix A[`i',1] = r(mean)
matrix A[`i',2] = r(p5)
matrix A[`i',3] = r(p95)
local ++i
}
matrix C = J(6,3,.)
local i = 1
foreach x in same_fake0U_ same_fake50U_ same_fake90U_ same_fake0U same_fake50U same_fake90U{
qui sum `x', d
matrix C[`i',1] = r(mean)
matrix C[`i',2] = r(p5)
matrix C[`i',3] = r(p95)
local ++i
}
matrix D = J(6,3,.)
local i = 1
foreach x in same_fake0L_ same_fake50L_ same_fake90L_ same_fake0L same_fake50L same_fake90L{
qui sum `x', d
matrix D[`i',1] = r(mean)
matrix D[`i',2] = r(p5)
matrix D[`i',3] = r(p95)
local ++i
}

matrix rownames A = "Probit (All)" "Probit ($\geq$50)" "Probit ($\geq$90)" "Logit (all)" "Logit ($\geq$50)" "Logit ($\geq$90)"
matrix rownames B = "Probit (All)" "Probit ($\geq$50)" "Probit ($\geq$90)" "Logit (all)" "Logit ($\geq$50)" "Logit ($\geq$90)"
matrix rownames C = "Probit (All)" "Probit ($\geq$50)" "Probit ($\geq$90)" "Logit (all)" "Logit ($\geq$50)" "Logit ($\geq$90)"
matrix rownames D = "Probit (All)" "Probit ($\geq$50)" "Probit ($\geq$90)" "Logit (all)" "Logit ($\geq$50)" "Logit ($\geq$90)"
matrix dcols = (0,0,1)
frmttable, statmat(A) substat(1) doubles(dcols) dbldiv(,) sq sdec(3) tex
frmttable, statmat(B) substat(1) doubles(dcols) dbldiv(,) sq sdec(3) merge tex 
frmttable, statmat(D) substat(1) doubles(dcols) dbldiv(,) sq sdec(3) merge tex 
frmttable, statmat(C) substat(1) doubles(dcols) dbldiv(,) sq sdec(3) merge tex 
frmttable using "~/Dropbox/China/Replication materials/TableA7.tex", ///
replace fr tex ctitle("","Lower bound","Upper bound","Lower bound","Upper bound") nobl la

************
* Figure 3 *
************
cd "~/Dropbox/China/Replication materials/"
use Dataset4, clear
***************
* Panel (c) *
***************
tab snotstranger more4 if abs(signeddistance)<4 & maxanswers==4 & Apartment+Apt_no!=.&first==1, exact
local pval = r(p_exact)
local pval = round(`pval',0.001)
twoway hist snotstranger if abs(signeddistance)<4 & maxanswers==4 & more4==0, lc(black) lw(thick) fc(none) percent s(0) disc||hist snotstranger if abs(signeddistance)<4 & maxanswers==4 & more4==1, percent lc(gray) fc(gray%10) graphregion(fc(white)) legend(order(1 "Apt per floor {&le} 4" 2 "Apt per floor > 4") size(large)) xti(Not a stranger,size(large)) ylab(,ang(0)) s(0) disc yti(Percent, size(large))
graph export "~/Dropbox/China/Replication materials/Figure3c.pdf", as(pdf) replace
***************************
* NETWORK CHARACTERISTICS *
***************************
************
* Figure 3 *
************
**************
* Panel (d) *
**************
cd "~/Dropbox/China/Replication materials/"
use Dataset4.dta, clear
keep if CommID+HHID!=. & Apartment+Apt_no!=.
cap drop product
gen product = CommID*building*Apartment*Apt_no
cap drop Paired
egen Paired = group(CommID building product)
cap drop CPaired
egen CPaired = count(HHID), by(Paired)
replace Paired = . if CPaired<=1
replace CPaired = . if CPaired<=1
replace product = . if CPaired==.
cap drop MPaired
egen MPaired = count(CPaired), by(CommID building Apartment)
cap drop minPaired
egen minPaired = min(product), by(CommID building Apartment)
cap drop maxPaired
egen maxPaired = max(product), by(CommID building Apartment)
replace maxPaired = . if minPaired==maxPaired
* First link
cap drop maxNotS1
egen maxNotS1 = max(notstranger) if product!=., by(product) missing
cap drop MaxNotS1
egen MaxNotS1 = max(maxNotS1), by(minPaired)
cap drop minNotS1
egen minNotS1 = min(notstranger) if product!=., by(product) missing
cap drop MinNotS1
egen MinNotS1 = min(minNotS1), by(minPaired)
cap drop MeanNotS1
egen MeanNotS1 = rmean(MaxNotS1 MinNotS1)
cap drop new1
gen new1 = minPaired*Apt_no
cap drop triad1
egen triad1 = group(new1 CommID building)
cap drop ctriad1
egen ctriad1 = count(Relationship), by(new1 CommID building)
cap drop Rtriad1
egen Rtriad1 = sum(notstranger) if Relationship!=., by(triad1)
replace Rtriad1 = . if ctriad1<=1
bys new1:gen laprimera = _n==1

tab MeanNotS1 Rtriad1 if more4==0&laprimera==1, exact
local pval0 = r(p_exact)
local pval0 = round(`pval0',0.001)
local fpval0 : display %4.3f `pval0'
tab MeanNotS1 Rtriad1 if more4==1&laprimera==1, exact
local pval1 = r(p_exact)
local pval1 = round(`pval1',0.001)
local fpval1 : display %4.3f `pval1'

cap drop LINKS
gen LINKS = MeanNotS1*2
matrix C1 = J(3,3,.)
matrix C2 = J(3,3,.)
local j = 1
foreach i in 0 1 2{
sum LINKS if Rtriad1==`i'&more4==0&laprimera==1
matrix C1[`j',1] = r(mean)
matrix C1[`j',2] = r(mean)-r(sd)/sqrt(r(N))
matrix C1[`j',3] = r(mean)+r(sd)/sqrt(r(N))
sum LINKS if Rtriad1==`i'&more4==1&laprimera==1
matrix C2[`j',1] = r(mean)
matrix C2[`j',2] = r(mean)-r(sd)/sqrt(r(N))
matrix C2[`j',3] = r(mean)+r(sd)/sqrt(r(N))
local ++j
}
*
coefplot (matrix(C1[,1]), ci((C1[,2] C1[,3])) label(Apt per floor {&le} 4) mc(gray) lw(thick) ciopt(lc(gray) lw(thick)) m(S) lp(dash) lc(gray)) ///
(matrix(C2[,1]), ci((C2[,2] C2[,3])) label(Apt per floor > 4) mc(black) ciopt(lc(black) lw(thick)) m(C) lp(solid) lc(solid)) ///
, vertical ylab(,ang(0)) ///
coeflab(r1="0" r2="1" r3="2")  ///
 xtitle("Directed links from i to k and j to k", size(large)) fc(none) connect(l l l) ytitle("Directed links between i & j", size(large)) graphregion(color(white)) legend(r(1)) offset(0) /*text(1.9 3 "p-value = `fpval0'") text(0.5 3 "p-value = `fpval1'")*/ legend(size(large))
graph export "~/Dropbox/China/Replication materials/Figure3d.pdf", as(pdf) replace

*********************************
* Calculation in text/footnotes *
*********************************
cd "~/Dropbox/china/Replication materials/"
use Dataset1, clear
gen Number = aptperfloor*floors_fixed
egen BB = group(commid building)
preserve
keep if counts!=.&treatment!=0
collapse (count) counts (max) Number, by(BB)
sum counts
di "Mean = " r(mean) " (s.d = " r(sd) " min = " r(min) " max = " r(max) ")"
gen perbuilding = counts/Number
sum perbuilding
di "Mean = " r(mean) " s.d = " r(sd)
restore
sum Number if counts!=.&treatment!=0
di "Mean = " r(mean) " s.d = " r(sd)
gen floor_t = floor(apartment/100)
gen floor_s = floor(apt_s/100) 
gen number_t = apartment-100*floor_t
gen number_s = apt_s-100*floor_s
gen T = 0 if floor_t==floor_s
replace T = 1 if floor_t<floor_s & number_t==number_s
replace T = 2 if floor_t<floor_s & number_t!=number_s
tab T if counts!=.&treatment!=0 
tab q8
sum oldneighbor if counts!=.&treatment!=0
tab counts if treatment!=0
correlate aptperfloor floors_fixed
global addcontr "relocated_sm yearmove_sm monthlyexp_sm onlyoneoffer_s oldtall_s oldapts_s"
reg response1 MaleC i.treatment i.commid i.piso#i.floors_fixed $addcontr aptperfloor samefloorE bothfriends if treatment!=0 & percentrelocated_fixed>=0 & (counts==1 | counts==2)
di _b[bothfriends] " " 1/_b[bothfriends]
* footnote 31
tab aptperfloor if counts!=.&treatment!=0
use Dataset2, clear
gen notstranger = 1-stranger
ttest notstranger, by(samefloors)
use Dataset4, clear
gen cuatro = snotstranger==4
ttest cuatro if abs(signeddistance)<4 & maxanswers==4 & first==1, by(more4)
di r(mu_1)/r(mu_2)-1


log using "variable description.log", replace
cd "~/Dropbox/China/Replication materials/"
use Dataset1.dta, clear
describe
use Dataset2.dta, clear
describe
use Dataset3.dta, clear
describe
use Dataset4.dta, clear
describe
use buildingcharacteristics, clear
describe
log close
